package com.hq.fxh.CustomerView.PopupWindow;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hq.fxh.R;
import com.hq.fxh.Utils.Tool;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description :
 * @Created 2017/9/15
 * @Email xlwenhao1858@sina.com
 */


public class PopupForHunYinSelect extends PopupWindow implements View.OnClickListener {


    protected View rootView;

    protected ListView popList;
    protected LinearLayout popupPingBi;
    protected ImageView userSexImageM;
    protected RelativeLayout userSexLayoutM;
    protected ImageView userSexImageW;
    protected RelativeLayout userSexLayoutW;
    protected TextView btnSexSetBack;
    protected TextView userSexTvM;
    protected TextView userSexTvW;
    protected TextView tvTitle;
    private Activity context;
    private View popView;
    PopSexCallBack modleCallBack;

    public PopupForHunYinSelect(Activity context, int sex, PopSexCallBack modleCallBack) {
        super(context);
        this.context = context;
        this.modleCallBack = modleCallBack;
        popView = LayoutInflater.from(context).inflate(R.layout.popup_sex_select, null);
        initView(popView);
        initData();
        if (sex == 0) {
            clickWoman();
        } else {
            clickMan();
        }

    }


    public void initData() {

        //设置PopupWindow的View
        this.setContentView(popView);
        //设置PopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置PopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置PopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setAnimationStyle(R.style.dialog_anim_style);
        this.setOutsideTouchable(false);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xffffffff);
    /*    openKeyboard();*/

        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                Tool.setAlpha(context, 1f);

            }
        });

    }


    private void initView(View rootView) {
        userSexImageM = (ImageView) rootView.findViewById(R.id.userSex_image_M);
        userSexLayoutM = (RelativeLayout) rootView.findViewById(R.id.userSex_layout_M);
        userSexLayoutM.setOnClickListener(PopupForHunYinSelect.this);
        userSexImageW = (ImageView) rootView.findViewById(R.id.userSex_image_W);
        userSexLayoutW = (RelativeLayout) rootView.findViewById(R.id.userSex_layout_W);
        userSexLayoutW.setOnClickListener(PopupForHunYinSelect.this);
        btnSexSetBack = (TextView) rootView.findViewById(R.id.btn_SexSet_Back);
        btnSexSetBack.setOnClickListener(PopupForHunYinSelect.this);
        userSexTvM = (TextView) rootView.findViewById(R.id.userSex_tv_M);
        userSexTvW = (TextView) rootView.findViewById(R.id.userSex_tv_W);
        userSexTvM.setText("未婚");
        userSexTvW.setText("已婚");
        tvTitle = (TextView) rootView.findViewById(R.id.tv_title);
        tvTitle.setText("婚姻状况");
    }


    public void show(View view) {

        showAtLocation(view, Gravity.CENTER, 0, 0);
        Tool.setAlpha(context, 0.7f);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.userSex_layout_M) {
            clickMan();
            modleCallBack.callSure(2, "已婚");
            dismiss();
        } else if (view.getId() == R.id.userSex_layout_W) {

            clickWoman();
            modleCallBack.callSure(1, "未婚");
            dismiss();

        } else if (view.getId() == R.id.btn_SexSet_Back) {
            dismiss();

        }
    }

    private void clickWoman() {

        userSexImageM.setVisibility(View.GONE);
        userSexImageW.setVisibility(View.VISIBLE);
        userSexTvW.setTextColor(context.getResources().getColor(R.color.blue));
        userSexTvM.setTextColor(context.getResources().getColor(R.color.textblack));

    }

    private void clickMan() {

        userSexImageM.setVisibility(View.VISIBLE);
        userSexImageW.setVisibility(View.GONE);
        userSexTvW.setTextColor(context.getResources().getColor(R.color.textblack));
        userSexTvM.setTextColor(context.getResources().getColor(R.color.blue));
    }


    public interface PopSexCallBack {
        public void callSure(int position, String comment);

    }


}
